﻿@using Pear.Web.Core
@inherits PearRazorPage<object>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@{ ViewData["Title"] = "菜单编辑";}
@section header
{

}
<form class="layui-form" action=""  lay-filter="security-edit">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <ul id="ParentSecurity" class="dtree" data-id="null"></ul>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" value="1" title="目录" checked>
                        <input type="radio" name="type" value="2" title="菜单">
                        <input type="radio" name="type" value="3" title="按钮">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入菜单名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">请求地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="url" autocomplete="off" placeholder="请输入请求地址" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">权限标识</label>
                    <div class="layui-input-block">
                        <input type="text" name="authorize" lay-verify="authorize" autocomplete="off" placeholder="请输入权限标识" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">菜单图标</label>
                    <div class="layui-input-block">
                        <input type="text" name="icon" id="icon" lay-filter="icon" autocomplete="off" placeholder="请输入菜单图标" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">显示排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sequence" lay-verify="sequence" autocomplete="off" placeholder="请输入排序号码" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="enabled" value="true" lay-skin="switch" lay-text="启用|禁用" lay-filter="enabled" checked />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea type="text" name="remark" lay-verify="remark" autocomplete="off" placeholder="请输入备注" class="layui-input layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="security-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
@section footer
{
    <script>
        layui.use(['form', 'jquery', 'http', 'popup', 'dtree','iconPicker'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let http = layui.http;
            let popup = layui.popup;
            let dtree = layui.dtree;
            let iconPicker = layui.iconPicker;




            var init_select = function (selectInitVal) {
                var tree = dtree.renderSelect({
                    elem: "#ParentSecurity",
                    initLevel: 4,
                    method: 'get',
                    url: "/api/security/all",
                    dataStyle: "layuiStyle",
                    dataFormat: "list",
                    line: true,
                    selectInitVal: `${selectInitVal}`,
                    response: {
                        statusName: "statusCode", //返回标识（必填）
                        statusCode: 200, //返回码（必填）
                        title: "name",
                    }
                });
            }

            iconPicker.render({
                elem: '#icon',
                type: 'fontClass',
                page: false
            });

            http.get('/api/security/profile/@GetValue("id","0")').then((result) => {
                form.val("security-edit", result.data);
                init_select(result.data.parentId);
                iconPicker.checkIcon('icon', result.data.icon);
            });


            form.on('submit(security-save)', function (data) {


                switch (data.field.type) {
                    case "2": data.field.type = 2; break;
                    case "3": data.field.type = 3; break;
                    default: data.field.type = 1; break;
                }

                data.field.parentId = data.field.ParentSecurity_select_nodeId || null;

                if (data.field.enabled == "true") {
                    data.field.enabled = true;
                } else {
                    data.field.enabled = false;
                }
                http.post('/api/security/@GetValue("id","0")/modify', JSON.stringify(data.field)).then((result) => {
                    if (result.succeeded) {
                        popup.success("保存成功", function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.refresh();
                        });
                    } else {
                        popup.failure("保存失败");
                    }
                });
                return false;
            });
        })
    </script>
}